﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Schools;
using PH.Logic.Schools;
namespace PH.Web.Management.Areas.School.Controllers
{
    [RouteArea("School")]
    [RoutePrefix("SchoolInfo")]
    [Authorize]
    public class SchoolInfoController : Controller
    {
        [Route("")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("Edit/{guid:guid?}")]
        public ActionResult Edit(Guid? guid)
        {
            SchoolModel school = null;
            if (guid != null)
                school = SchoolLogic.GetSchool(guid.Value);
            else
                school = new SchoolModel();
            return PartialView(school);

        }

        [Route("SetCurrent/{guid:guid}")]
        public ActionResult SetCurrent(Guid guid)
        {
            string errinfo = "对不起！操作失败，出错原因：";
            try
            {
                var school = SchoolLogic.GetSchool(guid);
                if (school != null)
                {
                    //PH.Web.Global.WebContext.CurrentSchool = school;
                    return Json(new PH.Model.Results.JsonResult<SchoolModel>(school));
                }
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return Json(new PH.Model.Results.JsonResult<SchoolModel>(null, errinfo, false));
        }

        [Route("SchoolList")]
        public ActionResult SchoolList()
        {
            var lst = SchoolLogic.GetSchools(null);
            return View(lst);
        }

        [Route("Schools")]
        public ActionResult Schools()
        {
            return View();
        }

        [Route("ExportSchoolCode/{bureauID:int}")]
        public ActionResult ExportScholCode(int bureauID)
        {
            try
            {
                SchoolQuery search = new SchoolQuery();
                search.BureauID = bureauID;
                var lst = SchoolLogic.GetSchools(search);
                var o = SchoolLogic.GetSchool(new Guid("E6E497D7-3CFF-4696-9002-5E58C07F0FA4"));
                lst.Insert(0, o);
                if (lst.Count > 0)
                {
                    Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                    workbook.Open(Server.MapPath("/RootFiles/Templates/schoolcode.xls"));
                    var cells = workbook.Worksheets[0].Cells;
                    for (var i = 0; i < lst.Count; i++)
                    {
                        var item = lst[i];
                        cells[i + 1, 0].PutValue(item.Code);
                        cells[i + 1, 1].PutValue(item.SchoolName);
                    }
                    var buffer = workbook.SaveToStream().ToArray();
                    return File(buffer, "application/octet-stream", "学校编码表.xls");
                }
                else
                    return Content("暂无数据");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }
    }
}
